{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 1002\n",
    "data_in_shape = (16, 16, 2)\n",
    "\n",
    "layers = [\n",
    "    Conv2D(5, (3,3), activation='relu', padding='same', strides=(2,2), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(4, (3,3), activation='linear', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(2, (1,1), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(3, (5,5), activation='relu', padding='same', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(2, (3,3), activation='linear', padding='same', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(4, (1,1), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(2, (3,3), activation='linear', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_02'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/02.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_02\": {\"input\": {\"data\": [-0.742023, -0.077688, -0.167692, 0.205448, -0.633864, -0.164175, -0.731823, 0.313236, 0.613465, -0.723716, -0.299231, 0.229032, 0.102561, 0.384949, -0.90948, -0.294898, -0.916217, -0.699031, -0.323329, -0.673445, 0.521949, -0.306796, -0.476018, -0.628623, 0.808028, -0.585043, -0.307429, -0.234868, -0.897584, 0.741743, 0.320785, 0.709132, -0.978084, 0.601894, -0.228816, -0.069558, -0.522066, -0.399597, -0.916222, 0.161549, -0.211915, 0.823372, -0.6549, -0.30403, 0.677588, -0.431259, 0.219659, -0.091937, -0.101636, -0.595218, -0.815428, 0.502932, 0.775249, 0.624226, 0.622601, -0.091075, 0.763603, 0.472659, 0.621131, -0.504549, -0.270214, 0.492749, 0.643055, -0.290058, -0.752162, 0.758918, 0.011832, -0.183967, 0.768298, 0.764241, 0.906398, 0.872853, -0.292238, 0.16788, -0.447741, 0.679196, 0.566614, 0.867549, -0.011606, -0.252108, 0.165669, -0.509362, 0.620632, -0.32465, -0.071143, -0.823613, 0.331067, -0.016903, -0.76138, -0.491146, 0.106088, -0.641492, 0.234893, 0.658853, -0.475623, 0.269103, 0.935505, -0.577134, 0.985015, -0.405957, -0.325882, 0.849518, -0.589155, 0.378331, -0.753075, 0.711411, 0.04547, 0.398327, -0.665657, 0.531142, -0.410293, -0.526649, 0.860648, 0.32795, -0.197082, -0.095526, -0.391361, 0.785465, -0.267269, -0.020154, -0.95189, -0.580742, 0.788104, -0.092433, 0.320354, 0.070651, 0.045416, 0.99799, 0.583116, -0.708131, -0.104784, -0.838947, -0.598224, 0.209105, 0.824956, 0.10438, 0.692046, -0.091308, 0.884896, 0.730617, 0.244486, -0.415624, -0.397714, -0.647236, -0.816162, 0.001325, 0.593873, -0.243723, 0.168275, 0.192345, -0.522916, 0.458154, -0.333828, -0.014549, -0.744552, -0.203297, 0.771256, -0.703928, 0.998892, -0.947633, 0.566086, -0.274437, -0.218108, -0.800599, 0.504541, 0.233776, -0.111802, -0.03089, 0.761595, 0.537963, 0.217941, -0.910822, 0.531235, -0.018533, -0.161811, -0.200401, -0.742618, -0.35126, -0.954474, -0.071092, -0.817604, 0.355039, -0.71623, -0.174142, -0.097041, -0.630172, -0.998405, 0.830425, -0.54058, 0.091193, -0.128435, -0.698893, -0.238618, 0.090807, -0.544714, 0.529065, 0.016035, 0.031572, -0.112153, -0.267803, 0.750932, -0.1755, -0.056039, -0.819933, 0.144761, 0.729724, -0.415998, 0.708302, -0.462218, -0.147249, 0.436382, -0.027364, 0.920436, 0.982113, -0.719826, -0.39192, 0.687992, 0.67207, 0.806184, -0.848625, -0.328676, 0.483859, 0.121646, -0.563886, -0.489969, 0.149126, 0.572393, -0.179969, 0.261244, 0.685893, 0.708577, 0.583762, 0.611609, 0.451507, 0.734521, 0.783842, 0.910886, 0.094794, -0.7603, -0.571009, 0.766552, -0.005378, 0.441925, -0.405844, -0.44351, -0.02502, 0.532842, -0.605724, 0.489149, -0.030616, 0.618367, -0.507044, 0.921095, 0.277124, 0.277796, 0.517938, -0.097656, -0.718449, -0.64776, -0.523526, -0.587764, -0.917412, -0.377526, -0.137101, -0.441038, -0.818358, -0.337538, -0.092447, -0.099825, 0.452412, -0.245787, -0.318709, 0.262443, 0.243089, -0.81751, 0.151959, -0.712124, -0.568058, 0.851689, 0.578001, 0.5535, -0.968162, 0.907764, -0.33327, 0.719366, 0.166725, 0.739587, -0.285609, -0.75929, 0.003963, 0.333512, 0.274331, 0.284225, 0.108992, -0.07236, 0.421666, 0.778074, -0.418525, -0.082178, 0.316198, -0.881106, 0.525714, -0.461855, 0.216712, 0.779531, 0.662279, 0.262508, 0.877733, 0.568056, 0.342805, -0.463299, -0.378632, 0.381052, 0.99444, -0.720164, -0.687681, -0.088427, 0.999846, 0.998652, 0.738574, 0.845422, 0.081389, -0.387621, 0.425191, -0.649076, -0.206885, 0.01809, -0.383956, 0.499222, -0.311785, 0.802452, -0.51268, 0.917756, 0.561026, -0.142752, 0.819264, -0.86242, -0.900524, -0.91049, -0.086446, -0.423479, 0.315488, -0.194078, 0.02387, -0.084516, -0.993568, -0.076955, 0.860088, -0.810888, -0.690519, 0.006994, -0.252601, 0.058697, 0.895512, -0.232657, -0.107581, -0.619655, -0.835333, 0.236688, -0.832395, -0.418488, 0.12863, 0.52645, 0.056954, 0.301947, -0.733774, -0.738797, 0.377858, -0.300689, 0.195755, 0.485844, 0.233366, -0.49798, 0.738907, 0.964552, 0.014276, -0.92375, 0.161384, -0.380637, 0.942824, -0.712071, 0.493943, -0.613403, 0.761721, 0.268988, 0.133175, -0.904693, -0.072221, -0.38943, -0.628801, -0.872558, 0.22282, 0.124402, 0.975161, -0.753868, -0.12336, -0.014669, -0.377103, 0.103176, -0.996724, 0.734126, -0.418102, 0.404471, -0.901678, -0.08642, 0.242373, -0.155506, 0.844976, -0.35723, 0.598496, -0.757695, 0.846844, 0.416975, -0.193873, -0.170191, 0.725291, 0.486349, -0.195375, 0.504815, 0.087869, 0.430875, 0.630802, 0.328853, 0.729598, -0.42338, 0.826289, 0.830184, -0.382228, 0.251766, -0.483812, -0.498886, -0.498876, 0.743145, -0.122468, 0.19787, 0.426367, -0.319919, 0.577979, 0.045291, -0.577872, -0.890138, -0.904327, 0.39408, 0.936017, 0.699018, -0.95137, 0.739907, -0.820321, 0.298268, 0.425665, -0.57465, -0.45691, -0.207231, 0.577525, 0.383161, 0.779462, 0.43675, -0.322722, 0.899672, -0.605107, -0.251538, -0.402769, -0.852449, 0.602925, 0.209488, -0.170528, -0.491783, -0.266771, -0.946184, -0.347622, -0.165897, -0.394488, -0.496513, 0.261219, -0.900065, -0.430779, -0.779167, -0.089611, -0.010344, 0.381565, 0.888566, -0.799082, -0.282598, -0.993037, 0.171011, 0.591692, -0.474342, -0.184681, 0.403464, -0.61967, -0.178805, 0.435739, -0.719391, -0.722968, 0.462124, -0.268135, -0.63873, -0.061148, 0.014527, -0.605344, -0.178504, -0.863282, -0.033531, -0.1711, -0.729127, 0.002226, 0.474697, 0.376, 0.234174, -0.900878, 0.779224, 0.188183], \"shape\": [16, 16, 2]}, \"weights\": [{\"data\": [-0.742023, -0.077688, -0.167692, 0.205448, -0.633864, -0.164175, -0.731823, 0.313236, 0.613465, -0.723716, -0.299231, 0.229032, 0.102561, 0.384949, -0.90948, -0.294898, -0.916217, -0.699031, -0.323329, -0.673445, 0.521949, -0.306796, -0.476018, -0.628623, 0.808028, -0.585043, -0.307429, -0.234868, -0.897584, 0.741743, 0.320785, 0.709132, -0.978084, 0.601894, -0.228816, -0.069558, -0.522066, -0.399597, -0.916222, 0.161549, -0.211915, 0.823372, -0.6549, -0.30403, 0.677588, -0.431259, 0.219659, -0.091937, -0.101636, -0.595218, -0.815428, 0.502932, 0.775249, 0.624226, 0.622601, -0.091075, 0.763603, 0.472659, 0.621131, -0.504549, -0.270214, 0.492749, 0.643055, -0.290058, -0.752162, 0.758918, 0.011832, -0.183967, 0.768298, 0.764241, 0.906398, 0.872853, -0.292238, 0.16788, -0.447741, 0.679196, 0.566614, 0.867549, -0.011606, -0.252108, 0.165669, -0.509362, 0.620632, -0.32465, -0.071143, -0.823613, 0.331067, -0.016903, -0.76138, -0.491146], \"shape\": [3, 3, 2, 5]}, {\"data\": [0.195612, -0.128132, -0.96626, 0.193375, 0.789956], \"shape\": [5]}, {\"data\": [-0.922097, 0.712992, 0.493001, 0.727856, 0.119969, -0.839034, -0.536727, -0.515472, 0.231, 0.214218, -0.791636, -0.148304, 0.309846, 0.742779, -0.123022, 0.427583, -0.882276, 0.818571, 0.043634, 0.454859, -0.007311, -0.744895, -0.368229, 0.324805, -0.388758, -0.556215, -0.542859, 0.685655, 0.350785, -0.312753, 0.591401, 0.95999, 0.136369, -0.58844, -0.506667, -0.208736, 0.548969, 0.653173, 0.128943, 0.180094, -0.16098, 0.208798, 0.666245, 0.347307, -0.384733, -0.88354, -0.328468, -0.515324, 0.479247, -0.360647, 0.09069, -0.221424, 0.091284, 0.202631, 0.208087, 0.582248, -0.164064, -0.925036, -0.678806, -0.212846, 0.960861, 0.536089, -0.038634, -0.473456, -0.409408, 0.620315, -0.873085, -0.695405, -0.024465, 0.762843, -0.928228, 0.557106, -0.65499, -0.918356, 0.815491, 0.996431, 0.115769, -0.751652, 0.075229, 0.969983, -0.80409, -0.080661, -0.644088, 0.160702, -0.486518, -0.09818, -0.191651, -0.961566, -0.238209, 0.260427, 0.085307, -0.664437, 0.458517, -0.824692, 0.312768, -0.253698, 0.761718, 0.551215, 0.566009, -0.85706, 0.687904, -0.283819, 0.5816, 0.820087, -0.028474, 0.588153, -0.221145, 0.049173, 0.529328, -0.359074, -0.463161, 0.493967, -0.852793, -0.552675, -0.695748, -0.178157, 0.477995, 0.858725, 0.120384, -0.515209, 0.204484, -0.025025, -0.654961, 0.239585, -0.654691, -0.651696, -0.699951, -0.054626, -0.232999, 0.464974, 0.285499, -0.311165, 0.18009, -0.100505, 0.303943, 0.265535, -0.960747, -0.542418, 0.195178, -0.848394, 0.0774, 0.250615, -0.690541, -0.106589, -0.587335, 0.52418, -0.750735, 0.906333, -0.185252, 0.091099, -0.516456, -0.314899, -0.398607, 0.555608, 0.741523, -0.454881, 0.5701, 0.205032, -0.772784, 0.733803, -0.669988, -0.872516, -0.124316, -0.664428, -0.363449, -0.511484, -0.192889, -0.91777, 0.017015, 0.158025, -0.407377, 0.62727, -0.121838, -0.930295, -0.139409, 0.758526, 0.711386, -0.371721, 0.833163, 0.179111], \"shape\": [3, 3, 5, 4]}, {\"data\": [0.318429, -0.858397, -0.059042, 0.68597], \"shape\": [4]}, {\"data\": [0.486255, -0.547151, 0.285068, 0.764711, 0.481398, 0.442527, -0.409304, 0.051033], \"shape\": [1, 1, 4, 2]}, {\"data\": [0.0965, 0.594443], \"shape\": [2]}, {\"data\": [0.228005, 0.859479, -0.49018, 0.232871, -0.303968, -0.799141, 0.621228, 0.850429, 0.029476, -0.583346, 0.889636, -0.128896, 0.067108, -0.1059, -0.788773, -0.559347, 0.674802, 0.513275, -0.95495, -0.230976, -0.430566, 0.607782, -0.292593, -0.362274, -0.825576, 0.904458, 0.531651, 0.139053, -0.797761, 0.905804, -0.875903, 0.04377, -0.704592, 0.203555, -0.083031, 0.321316, 0.334565, 0.965166, 0.31912, 0.987618, 0.11275, 0.755438, 0.133156, -0.271605, -0.739053, 0.930942, 0.723852, -0.399546, 0.365907, 0.680404, 0.302211, 0.481088, -0.254831, -0.719056, -0.13153, 0.195489, -0.457555, -0.825431, -0.866045, -0.010333, -0.168299, 0.986557, -0.410139, 0.607414, 0.897455, -0.503628, -0.125592, -0.216501, 0.854905, 0.862831, 0.911679, 0.451035, -0.208763, -0.0935, -0.482974, -0.552539, -0.348091, -0.720601, -0.234177, -0.522557, -0.128204, 0.992609, -0.112888, -0.806388, 0.223532, -0.205692, -0.772152, 0.989159, -0.277926, -0.544355, -0.707034, -0.598198, 0.555265, -0.382095, -0.372491, 0.631353, 0.784895, -0.373885, -0.368449, 0.898173, 0.252309, 0.912245, -0.033641, -0.896843, 0.914818, -0.598634, -0.389127, 0.116656, -0.476051, -0.595, 0.076412, -0.998532, -0.469193, -0.993491, 0.503043, 0.698487, 0.538773, -0.763721, 0.048814, -0.996688, -0.13339, 0.032822, 0.777114, -0.102801, -0.866702, -0.544717, -0.870035, -0.645622, -0.235138, 0.178248, 0.593266, 0.562264, -0.042026, -0.019967, -0.330309, 0.349891, 0.858278, 0.350973, -0.639028, -0.779616, 0.548236, 0.011033, -0.678063, 0.361407, 0.625538, -0.380135, -0.531038, 0.255256, -0.552471, 0.188966], \"shape\": [5, 5, 2, 3]}, {\"data\": [0.229761, -0.670851, -0.398017], \"shape\": [3]}, {\"data\": [-0.211487, -0.648815, -0.854588, -0.616238, -0.200391, -0.163753, 0.525164, 0.04282, -0.178234, 0.074889, -0.458875, -0.133347, 0.654533, -0.456294, 0.454776, -0.799519, -0.004428, 0.160632, 0.153349, -0.585922, -0.407693, 0.794725, -0.535387, 0.408942, -0.182012, 0.741361, 0.045939, -0.156736, -0.156846, -0.357358, 0.539258, 0.948017, -0.307682, -0.715505, 0.740323, 0.616044, -0.79421, 0.478351, -0.40107, 0.597915, -0.251741, -0.56835, -0.30559, 0.943538, -0.121007, -0.5726, 0.63163, -0.4633, -0.466873, -0.269675, 0.939682, -0.088032, -0.686666, -0.763883], \"shape\": [3, 3, 3, 2]}, {\"data\": [0.311362, -0.228519], \"shape\": [2]}, {\"data\": [-0.946541, 0.585593, -0.49527, 0.594532, -0.38557, 0.124664, 0.290608, 0.016082], \"shape\": [1, 1, 2, 4]}, {\"data\": [0.114077, -0.889658, -0.472025, 0.718808], \"shape\": [4]}, {\"data\": [-0.536401, 0.404425, -0.338344, -0.818131, 0.441566, 0.867769, 0.505219, 0.689963, 0.598858, 0.268687, -0.728914, 0.878801, -0.318668, -0.305686, -0.80848, 0.195743, 0.821773, 0.716334, -0.41629, 0.169864, -0.799399, -0.27582, 0.312052, -0.021639, 0.822725, -0.417215, 0.246061, -0.188668, -0.632463, 0.781629, -0.462136, -0.881432, -0.024391, 0.94485, -0.300768, 0.615408, 0.12297, 0.540219, 0.335681, -0.355003, 0.194186, 0.922101, -0.592691, -0.345502, 0.812393, 0.922283, 0.520438, 0.878666, -0.380913, 0.41579, -0.541346, -0.72511, -0.276467, 0.539221, 0.09016, 0.344573, 0.564725, 0.392841, -0.250152, -0.200319, 0.615438, 0.658222, -0.706144, 0.066833, -0.346988, 0.490674, 0.566333, -0.210763, -0.292341, 0.583961, 0.007116, -0.958503], \"shape\": [3, 3, 4, 2]}, {\"data\": [0.255023, -0.721247], \"shape\": [2]}], \"expected\": {\"data\": [-3.226907, 2.897466, 4.614365, 3.29115, 2.999473, 6.406079, -0.957869, 13.266747, 1.091891, 3.802161, -1.217917, 8.808489, 5.691413, 2.603114, -1.765725, 10.413739, 7.91112, 7.984789, 5.843855, 11.019684, -1.760968, 11.507448, -3.58255, 24.304056, -5.537182, -0.779647, -5.423543, -0.766776, -2.797846, 8.630902, 3.297426, 30.247473], \"shape\": [4, 4, 2]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
